package org.uim.cricket.util;

/**
 * The Class CricketFunctions.
 */
public class CricketFunctions {

	/** The Constant BALLS_PER_OVER. */
	public static final int BALLS_PER_OVER = 6;

	/**
	 * 
	 * Checks two names to verify whether they both are same.
	 * 
	 * @param searchIn The name which should be searched in. Usually this can be the long name.
	 * @param searchFor The name which searched for.
	 * @return true if both players are same, otherwise false.
	 */
	public boolean arePlayersSame(String searchIn, String searchFor) {
	  
	    searchIn = " " + searchIn + " ";
	    searchFor = " " + searchFor + " ";
	    if (searchIn.indexOf(searchFor) >= 0) return true;
	    return false;
	    
	}
	
	/**
	 * Converts balls to overs.
	 * 
	 * @param balls
	 *            the balls
	 * @return the string representing overs
	 */
	public String ballsToOvers(int balls) {
		if (balls <= 0)
			return "0";
		if (balls % BALLS_PER_OVER == 0) {
			return String.valueOf(balls / BALLS_PER_OVER);
		} else {
			return String.valueOf(balls / BALLS_PER_OVER) + "."
					+ String.valueOf(balls % BALLS_PER_OVER);
		}
	}

	/**
	 * Overs to balls.
	 * 
	 * @param overs
	 *            the overs
	 * @return the number of balls for given overs as int.
	 */
	public int oversToBalls(String overs) {
		int pos = overs.indexOf('.');
		if (pos < 0) {
			return Integer.parseInt(overs) * BALLS_PER_OVER;
		}
		return Integer.parseInt(overs.substring(0, pos)) * BALLS_PER_OVER
				+ Integer.parseInt(overs.substring(pos + 1));
	}

	/**
	 * Gets an average of batting assuming zero notout innings.
	 * 
	 * @param innings total number of innings played
	 * @param runs total number of runs scored
	 * @return average batting.
	 */
	public double battingAverage(int innings, int runs) {
		return this.battingAverage(innings, 0, runs);
	}
	
	/**
	 * Gets an average of batting.
	 * 
	 * @param innings total number of innings played
	 * @param notouts number of times player was notout
	 * @param runs total number of runs scored
	 * @return average batting.
	 */
	public double battingAverage(int innings, int notouts, int runs) {
		if (innings - notouts <= 0) return -1.0;
		return (runs / ((innings - notouts) * 1.0));
	}
	
	/**
	 * Gets the bowling average.
	 * 
	 * @param wickets total wickets taken
	 * @param runs total runs conceeded
	 * @return the bowling average.
	 */
	public double bowlingAverage(int wickets, int runs) {
		if (wickets <= 0) return -1.0;
		return (runs / (wickets * 1.0));
	}
	
	/**
	 * Gets an economy rate of a bowler.
	 * 
	 * @param runs total runs conceeded.
	 * @param overs total overs bowled.
	 * @return the economy rate.
	 */
	public double economyRate(int runs, String overs) {
		int balls = this.oversToBalls(overs);
		return this.economyRate(runs, balls);
	}
	
	/**
	 * Gets an economy rate of a bowler.
	 * 
	 * @param runs total runs conceeded
	 * @param balls total balls bowled
	 * @return the economy rate.
	 */
	public double economyRate(int runs, int balls) {
		if (balls <= 0) return -1.0;
		return (runs / (balls * 1.0)) * BALLS_PER_OVER;
	}
	
	/**
	 * 
	 * Gets how many balls are in between the two overs.
	 * 
	 * @param startOver start over as a String
	 * @param endOver end over as a String
	 * @return Number of balls in between startOver exclusive and endOver inclusive.
	 */
	public int getOverDifference(String startOver, String endOver) {
	    int b1 = this.oversToBalls(startOver);
	    int b2 = this.oversToBalls(endOver);
	    
	    return b2 - b1;
	}
	
	/**
	 * 
	 * Gets how many overs in between startOver and endOver. 
	 * 
	 * @param startOver start over as a String
	 * @param endOver end over as a String
	 * @return Overs in between startOver exclusive and endOver inclusive.
	 */
	public String getOverDifferenceInOvers(String startOver, String endOver) {
	    int balls = this.getOverDifference(startOver, endOver);
	    return this.ballsToOvers(balls);
	}
	
	/**
	 * Gets the complement team to the teamToFind parameter. As and example,
	 * if team1 = "A", team2 = "B" and teamToFind = "B" then this function
	 * returns "A" as the result, because complement team for "B" is "A".
	 * If teamToFind is neither team1 nor team2, it will return null.
	 * 
	 * @param team1 First team
	 * @param team2 Second team
	 * @param teamToFind the team name to be found
	 * @return complement of the given team to be found. If given team can't be
	 *          found then it will return null.
	 */
	public String getComplementTeam(String team1, String team2, String teamToFind) {
		if (team1.equals(teamToFind)) return team2;
		if (team2.equals(teamToFind)) return team1;
		return null;
	}
}